<?php
require ("utils.php");
//=====================================================language=============================================
if($_SESSION['langname']=="")
{
	$language_url="languages/"."english".'/cart_add_language.php';
}
else
{
	$language_url="languages/".$_SESSION['langname'].'/cart_add_language.php';
}

include_once("$language_url");
//======================================================End============================================
if($_SESSION['siteuserId']=="")
{
	header("location:start.php");
}
$value	=	$_REQUEST['mode'];
switch($_REQUEST['mode'])
{
	case	update_cart:	update_cart();
							break;
	case 	delete_meals:	delete_meals();
							break;	
	default:				main_content("main();");
}
						 								
function main()
{

		$sql_fetch_temp		=	mysql_query("select * from tblordertemp where session_id='".session_id()."' and siteuserId='".$_SESSION['siteuserId']."' ");
	
	$cnt_temp			=	mysql_num_rows($sql_fetch_temp);
	$rec_total			=	mysql_fetch_array(mysql_query("select sum(total_amount) as TOTAL from tblordertemp where session_id='".session_id()."'"));					 
	
?><script language="JavaScript">
function UpdateCart()
{
	var counttemp	=	document.viewCart.cnt.value;
//alert(counttemp);
	for(var i=1;i<=counttemp;i++)
	{
		if(document.getElementById("quantity"+i).value.search(/\S/)==-1)
		{
		//alert("Please enter add quantity");
		document.getElementById('text_err').innerHTML="<?php echo ADD_QUANTITY; ?>";
		document.getElementById("quantity"+i).select();
		return false;
		}
		if(isNaN(document.getElementById("quantity"+i).value))
		{
		//alert("Please enter correct add quantity");
		document.getElementById('text_err').innerHTML="<?php echo CORRECT_ADD_QUANTITY; ?>.";
		document.getElementById("quantity"+i).select();
		return false;
		}
		if(document.getElementById("quantity"+i).value<=0)
		{
		document.getElementById('text_err').innerHTML="<?php echo NEGATIVE_QUANTITY; ?>.";
		//alert("Please Do not enter negative add quantity");
		document.getElementById("quantity"+i).select();
		return false;
		}
	}
	document.viewCart.mode.value="update_cart";
	document.viewCart.submit();
}
function checkAll(){
	for (var i=0;i<document.viewCart.elements.length;i++)
	{
		var e=document.viewCart.elements[i];
		if ((e.name != 'remove_meals') && (e.type=='checkbox'))
		{
			e.checked=document.viewCart.remove_meals.checked;
		}
	}
	}
function removeCart()
{
	var isSelected = false;
	for(i=1;i<document.viewCart.elements.length;i++){
	if(document.viewCart.elements[i].type=="checkbox" && document.viewCart.elements[i].checked){
	isSelected = true;
	break;
	}
	}
	if(!isSelected){
	alert("Please select at least one add");
	return;
	}
	if(window.confirm("Do you really want to delete this(s) add?") == true)
	{
		
		document.viewCart.mode.value='delete_meals';
		document.viewCart.submit();
	}

}
function proceed()
{
	document.viewCart.mode.value='proceed';
	document.viewCart.submit();
}
function LoginCheck()
{
	if(document.viewCart.username.value.search(/\S/)==-1)
	{
		alert("Please Enter Your User Name");
		document.viewCart.username.select();
		return false;
	}
	if(document.viewCart.password.value.search(/\S/)==-1)
	{
		alert("Please Enter Your Password");
		document.viewCart.password.select();
		return false;
	}
	document.viewCart.mode.value="loginAgain";
	document.viewCart.submit();
	return true;
}	
</script><script language="javascript">
	function checkField()
	{
		if((document.viewCart.quantity.value.search(/\S/)==-1) || (document.viewCart.quantity.value<=0))
		{
			alert("Please Enter The Correct Quantity");
			document.viewCart.quantity.focus();
			return false;
		}
		if(isNaN(document.viewCart.quantity.value)==true)
		{
			alert("The Quantity Should be Numeric");
			document.viewCart.quantity.focus();
			return false;
		}
		document.viewCart.submit();
	}
</script>
<!--<form name="viewCart" action="<?php// echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="mode">
<input type="hidden" name="cnt" value="<?php// echo $cnt_temp; ?>">-->
<table width="90%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td valign="top" align="center"><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
                  <tr>
                    <td width="20%" valign="top"><?php include("left_pannel.php"); ?></td>
                    <td style="width:10px;">&nbsp;</td>
                    <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td width="11"><img src="images/PinkBorderTopLeft.jpg" alt="" width="11" height="11"></td>
                              <td class="PinkBoxTopBg"><img src="images/spacer.gif" alt="" width="10" height="11"></td>
                              <td width="11"><img src="images/PinkBorderTopRight.jpg" alt="" width="11" height="11"></td>
                            </tr>
                        </table></td>
                      </tr>
                      <tr>
                        <td class="PinkTwosideBorder" valign="top">
<form name="viewCart" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="mode">
<input type="hidden" name="cnt" value="<?php echo $cnt_temp; ?>">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td class="blueshadow"><img src="images/spacer.gif" alt="" width="1" height="12" /></td>
	</tr>
	<tr>
		<td class="padding16">
			<table width="100%" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td style="padding-left:20px;"><h1> <?php echo SHOPPING_CART; ?></h1></td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td align="center"><table align=center width="95%" border="0" cellspacing="0" cellpadding="5" class="tblBorder">
			  <tr>
				<td style="padding-left:300px;" class="ERR"><?php echo $GLOBALS['err']; ?></td>
			  </tr>
			<tr>
				<td style="padding-left:300px;" class="ERR"><font id="text_err"></font></td>
			  </tr>
			  <?php
			  	if($cnt_temp>0)
				{
			  ?>
			<tr>
				<td align="right" nowrap="true" style="padding-bottom:5px;"><b class="largest"><?php echo REMOVE_ALL; ?></b>&nbsp;<input type="checkbox" name="remove_meals" onClick="checkAll();"></td>
			</tr>
			<?php
				}
			?>
<tr>
<td>
<table align="center" cellpadding="0" cellspacing="0" border="1" width="100%">
<TR><TD>
<table align="center" cellpadding="1" cellspacing="1" border="0" width="100%" class="login_txt">
<tr>
				<td class="listHeader" align="center"><?php echo ADD_TITLE; ?></td>
				<td class="listHeader" align="center">Product</td>
				<td class="listHeader" align="center"><?php echo UNIT_PRICE; ?></td>
				<!--<td class="listHeader" align="center"><?php echo QUANTITY; ?></td>-->
				<td class="listHeader" align="center"><?php echo CART_PRICE; ?></td>
				<td class="listHeader" align="center"><?php echo REMOVE; ?></td>
			</tr>
			  <?php
			  if($cnt_temp<=0)
			  {
			  ?>
			  <tr>
				<td style="padding-left:300px;" class="ERRRED" valign="middle" height="35"><?php echo YOUR_CART_EMPTY; ?></td>
			  </tr>
			  <?php
			  }
			  else
			  {
				  $i=1;
					$loop=0;
				  while($rec_fetch_temp	=	mysql_fetch_array($sql_fetch_temp))
				  {

					$rec_add	=	mysql_fetch_array(mysql_query("select * from advertise_master AM, advertise_details AD where AD.advertiseId = AM.advertiseId and AM.advertiseId='".$rec_fetch_temp['advertiseId']."' "));
					$loop++;
					if($loop%2 == 0) 
					{
					$class1="normalActive2";
					$class2="normal2";
					$class3="normal2";
					$class4="gray_sitename2";
					}
					else 
					{
					$class1="normalActive";
					$class2="normal";
					$class3="normal";
					$class4="white_sitename2";
					}

			  ?>
	  		<tr onmouseover="this.className='<?php echo $class1; ?>'" onmouseout="this.className='<?php echo $class2; ?>'" class="<?php echo $class3; ?>">
				<td align="center" class="<?php echo $class4; ?>"><?php echo stripslashes($rec_add['adTitle_personal']); ?>&nbsp;</td>
				<td align="center" class="<?php echo $class4; ?>"><?php echo stripslashes($rec_add['adDisplayType']); ?>&nbsp;</td>
				<!--<td align="center" class="<?php// echo $class4; ?>"><?php// echo $rec_add['startDate']; ?>-<?php// echo $rec_add['endDate']; ?>&nbsp;</td>-->
				<td align="center" class="<?php echo $class4; ?>">
				$<?php echo stripslashes($rec_fetch_temp['adspace_price']); ?>&nbsp;
				</td>
				<!--<td class="<?php echo $class4; ?>" align="center">
				<input type="text" name="quantity_<?php echo $rec_fetch_temp['temp_id']; ?>" value="<?php echo stripslashes($rec_fetch_temp['Quentity']); ?>" size="5" id="quantity<?php echo $i; ?>" class="InputBox" style="width:50px;">
                          bb
                        </td>-->
				<td align="center" class="<?php echo $class4; ?>">
				<?php echo stripslashes($rec_fetch_temp['total_amount']); ?>
				</td>
				<td class="<?php echo $class4; ?>" align="center">
				<input type="checkbox" name="remove_meals[]" value="<?php echo stripslashes($rec_fetch_temp['temp_id']); ?>">
				</td>
			</tr>
			<?php
				  $i++;
				}
			?>
			<tr>
				<td class="ERRRED" colspan="3"><?php echo SUBTOTAL; ?> : </td>
                                <td class="ERRRED" align="left" style="color:red;"><?php echo $rec_total['TOTAL']; ?></td>
				<!--<td>&nbsp;</td>-->
			</tr>
			<?php
			} 
			?>
</table>
</TD></TR></table>
</TD></TR>

		</table>

		</td>
	</tr>
	<tr>
		<td align="center">
			<table align=center width="95%"  border="0" cellspacing="0" cellpadding="0">
			  <?php
					if($cnt_temp>0)
					{
			  ?>
				<tr>
					<td style="padding:5px 0px 5px 1px; text-align:right;border:solid 0px;"><input type="button" value="<?php echo REMOVE; ?>" onClick="removeCart();" class="button" align="right">
					</td>
				</tr>
				<!--<tr>
					<td style="padding:5px 0px 5px 1px; text-align:right;border:solid 0px;"><input class="button" type="button" name="update_cart" value="<?php// echo UPDATE_CART; ?>" onClick="UpdateCart();">
					</td>
				</tr>-->
				<?php
					}
				?>
				<tr>
					<td style="padding:5px 0px 5px 1px; text-align:right;border:solid 0px;"><input <?php if(mysql_num_rows($sql_fetch_temp)==0){ ?> disabled = "true" <?php } ?>  type="button" name="checkout" value="<?php echo CHECKOUT_NOW; ?>" onClick="javascript:document.location='checkout.php?tab=buy_ads&subMenu=1'" class="button">&nbsp;<input class="button" type="button" name="continue_shopping" value="<?php echo CONTINUE_SHOPPING; ?>" onclick="javascript:document.location='buy_spaceNew.php?tab=buy_ads&subMenu=1'"><br /><br />
					<?php if($cnt_temp>0) {?><hr style="height:1px;" /><?php }?>
					</td>
				</tr>
			</table>
		</td>
	</tr>
	
	<tr>
		<td><BR>&nbsp;</td>
	</tr>
</table>
</form>

</td>
                      </tr>
                      <tr>
                        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td width="11"><img src="images/PinkBorderBottomLeft.jpg" alt="" width="11" height="11"></td>
                              <td class="PinkBoxBottomBg"><img src="images/spacer.gif" alt="" width="10" height="11"></td>
                              <td width="11"><img src="images/PinkBorderBottomRight.jpg" alt="" width="11" height="11"></td>
                            </tr>
                        </table></td>
                      </tr>
                    </table></td>
                    <td>&nbsp;</td>
                    </tr>
                </table></td>
              </tr>
            </table>
<!--</form>-->
            
<?php
}
function update_cart()
{

$rs_select_total=mysql_query("select * from tblordertemp");
while($row_select_total=mysql_fetch_array($rs_select_total))
{
$temp_id=$row_select_total[temp_id];

 $quantity_update=$_POST['quantity_'.$temp_id];
	if($quantity_update!="")
	{
		if($row_select_total[meals_qty]!=$quantity_update)
		{
			 $total_amount_update=$quantity_update*$row_select_total['adspace_price'];
			 $sql_update="update tblordertemp set
			 			 Quentity='".$quantity_update."',
						 total_amount='".$total_amount_update."'
						 where temp_id='".$row_select_total[temp_id]."'";
			 mysql_query($sql_update);	
			$GLOBALS['err']	= UPDATE_CART;	
		}
	}
}
main_content("main();");
}
function delete_meals()
{
	foreach($_REQUEST['remove_meals'] as $value)
	{
		$sql_delete	=	"delete from tblordertemp where temp_id='".$value."'";
		$query_delete	=	mysql_query($sql_delete);
		
	}
	$GLOBALS['err']	= DELETE_CART;
	main_content("main();");	
}

?>
